package com.test.tree;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Boyu on 2017/3/21.
 */
public class ParseTree {


    /**
     * 使用递归方式构造树结构
     * @param treeNodeList
     * @param <T>
     * @return
     */
    public static <T> Tree<T> buildTree(List<Tree<T>> treeNodeList){

        if(treeNodeList == null)
        {
            return null;
        }

        List<Tree<T>> rootList = new ArrayList<>();

        for(Tree<T> node: treeNodeList)
        {
            if(node.getParentId()==null || node.getParentId() == "")
            {
               rootList.add(node);
               node.setChildren(getChildren(node.getId(),treeNodeList));
            }
        }


        Tree<T> rootNode = new Tree<>();

        if(rootList.size() == 1)
        {
            rootNode = rootList.get(0);
        }else {
            rootNode.setId("-1");
            rootNode.setText("根节点");
            rootNode.setChildren(treeNodeList);
        }

        return rootNode;
    };

    public static final <T> List<Tree<T>> getChildren(String pid, List<Tree<T>> treeNodeList)
    {
        List<Tree<T>> childList = new ArrayList<>();
        for(Tree<T> node : treeNodeList)
        {
            if(node.getParentId()==null || node.getParentId() == "")
                continue;
            if(node.getParentId().equals(pid))
            {
                childList.add(node);
                node.setChildren(getChildren(node.getId(), treeNodeList));
            }

        }
        return childList;
    }

    public static <T> Tree<T> buildTreeNoCur(List<Tree<T>> treeList)
    {

        if(treeList ==null)
            return null;

       List<Tree<T>> rootNodeList = new ArrayList<>();

       for(Tree<T> treeNode : treeList)
       {
           String pid = treeNode.getParentId();
           if(pid==null || pid=="")
           {
               rootNodeList.add(treeNode);
               continue;
           }

           for(Tree<T> parent: treeList)
           {
               String id = parent.getId();
               if(id!=null && id.equals(pid))
               {
                   parent.getChildren().add(treeNode);
                   parent.setChildren(true);
                   treeNode.setParent(true);

                   continue;
               }
           }

       }

        Tree<T> rootNode = new Tree<>();

        if(rootNodeList.size() == 1)
        {
            rootNode = rootNodeList.get(0);
        }else {
            rootNode.setId("-1");
            rootNode.setText("根节点");
            rootNode.setParent(false);
            rootNode.setChildren(true);
            rootNode.setChildren(rootNodeList);
        }

        return rootNode;


    }


    public static <T> void parseTreeNode(Tree<T> rootNode, int index)
    {
       if(rootNode == null)
           return;
       for(int i=0;i<index;i++)
       {
           System.out.print("--");
       }
        System.out.println(rootNode.getText());
        if(rootNode.getChildren().size()>0)
        {
            index++;
            for(Tree<T> childNode : rootNode.getChildren())
            {
                parseTreeNode(childNode, index);
            }
        }

    }


}
